#include <stdio.h>
#include <stdlib.h>
#include "mygetline.h"

/*@i:记录字符串长度
 *@ch:读取字符
 *
 */

ssize_t mygetline(char **lineptr,size_t *n,FILE *stream)
{

	ssize_t ch,i = 0;

	if(*lineptr == NULL || *n == 0)
	{
		*n = 10;
		*lineptr = malloc(sizeof(*n));
		if(lineptr == NULL)
		{
			return -1;
		}
	}
	
	
	while((ch = fgetc(stream)) != '\n')
	{
 		if(ch == EOF)
			return -2;
//预留'\n'和'\0',每次分配10个字节的空间		
		if(i >= *n-2)
		{
			*n += 10;
			*lineptr = realloc(*lineptr,*n);
		}
		*(*lineptr+i++) = ch;
	}
	*(*lineptr+i++) = '\n';
	*(*lineptr+i) = '\0';
	return i;
}
